package com.tools.web.sms;

import com.tools.common.object.Note;
import com.tools.common.object.string.VerifyKit;
import com.tools.web.http.HttpBuilder;
import com.tools.web.http.HttpResult;

import java.util.Objects;

/**
 * 网建短信通的配置实体类
 * */
@Note("网建短信通的配置实体类")
public final class NetSms {

    @Note("你在网建注册的用户名")
    private String uid;

    @Note("密钥")
    private String key;

    @Note("UTF-8 编码的服务接口")
    private String urlUtf8Service;

    @Note("GBK 编码的服务接口")
    private String urlGbkService;

    public NetSms() {}

    public NetSms(String uid, String key) {
        this.setUid(uid);
        this.setKey(key);
    }


    public String getUid() {
        if(uid == null || uid.isEmpty()) throw new NullPointerException("用户名为空");
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getKey() {
        if(key == null || key.isEmpty()) throw new NullPointerException("密钥为空");
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getUrlUtf8Service() {
        if(urlUtf8Service == null || urlUtf8Service.isEmpty()) throw new NullPointerException("UTF-8 编码的服务接口地址为空");
        return urlUtf8Service;
    }

    public void setUrlUtf8Service(String urlUtf8Service) {
        this.urlUtf8Service = urlUtf8Service;
    }

    public String getUrlGbkService() {
        if(urlGbkService == null || urlGbkService.isEmpty()) throw new NullPointerException("GBK 编码的服务接口地址为空");
        return urlGbkService;
    }

    public void setUrlGbkService(String urlGbkService) {
        this.urlGbkService = urlGbkService;
    }

    @Override
    public String toString() {
        return "NetSms{" +
                "uid='" + uid + '\'' +
                ", key='" + key + '\'' +
                ", urlUtf8Service='" + urlUtf8Service + '\'' +
                ", urlGbkService='" + urlGbkService + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        NetSms netSms = (NetSms) o;
        return Objects.equals(uid, netSms.uid) && Objects.equals(key, netSms.key);
    }

    @Override
    public int hashCode() {
        return Objects.hash(uid, key);
    }


    @Note("以 GBK 的编码请求发送短信")
    public String sendWithGBK(String mobile, String content) {
        return private_sendAndGetResult(mobile, content, this.getUrlGbkService());
    }

    @Note("以 UTF-8 的编码请求发送短信")
    public String sendWithUTF8(String mobile, String content) {
        return private_sendAndGetResult(mobile, content, this.getUrlUtf8Service());
    }


    @Note("向网建服务发送 HTTP 请求")
    private String private_sendAndGetResult(String mobile, String content, String url) {
        if(!VerifyKit.isPhoneStrict(mobile)) throw new IllegalArgumentException("手机号码格式不正确");
        if(content == null || content.isEmpty()) throw new IllegalArgumentException("要发送的短信内容为空");
        String uid = this.getUid();
        String key = this.getKey();
        HttpResult result = HttpBuilder.of(url)
                .param("uid", uid)
                .param("key", key)
                .param("smsMob", mobile)
                .param("smsText", content)
                .executePost();
        return result.getContentString();
    }
}
